package ass.dao.support;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import ass.beans.ReviewBean;
import ass.common.DBConnectionFactory;
import ass.common.ServiceLocatorException;
import ass.dao.DataAccessException;
import ass.dao.ReviewDAO;

public class ReviewDAOImpl implements ReviewDAO {
	
	private DBConnectionFactory services;
	
	public ReviewDAOImpl() {
		try {
			services = new DBConnectionFactory();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
		}
	}

	public ReviewDAOImpl(DBConnectionFactory services) {
		this.services = services;
	}
	
	public void insert(ReviewBean ReviewBean) throws DataAccessException{
		
		Connection con = null;
		   try { 
		 con = services.createConnection();
	     PreparedStatement stmt = con.prepareStatement(
	       "INSERT INTO TBL_REVIEWS (movie_id, nickname, review, rating, date_added) values (?,?,?,?,?)");
	     stmt.setInt(1, ReviewBean.getMovie_id());
	     stmt.setString(2, ReviewBean.getNickname());
	     stmt.setString(3, ReviewBean.getReview());
	     stmt.setInt(4, ReviewBean.getRating());
	     stmt.setDate(5, ReviewBean.getDate_added());
	     
	     int n = stmt.executeUpdate();

	     stmt.close();
	     if (n != 1)
	       throw new DataAccessException("Did not insert one row into database");
	   } catch (ServiceLocatorException e) {
	       throw new DataAccessException("Unable to retrieve connection; " + e.getMessage(), e);
	   } catch (SQLException e) {
	       throw new DataAccessException("Unable to execute query; " + e.getMessage(), e);
	   } finally {
	      if (con != null) {
	         try {
	           con.close();
	         } catch (SQLException e1) {
	           e1.printStackTrace();
	         }
	      }
	   }
		
		
	}
	
	public List<ReviewBean> getReviewByMovie (int movie_id) throws DataAccessException{
		List<ReviewBean> list = new ArrayList<ReviewBean>();
		Connection con = null;
		try {
			con = services.createConnection();

			PreparedStatement stmt = con
					.prepareStatement(  
										" select * from tbl_reviews where movie_id = ?");
			stmt.setInt(1, movie_id);

			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				ReviewBean movieBean = createReviewBean(rs);
				list.add(movieBean);
			}
			stmt.close();
		} catch (ServiceLocatorException e) {
			throw new DataAccessException("Unable to retrieve connection; "
					+ e.getMessage(), e);
		} catch (SQLException e) {
			throw new DataAccessException("Unable to execute query; "
					+ e.getMessage(), e);
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		}

		return list;
	}
	
	private ReviewBean createReviewBean(ResultSet rs) throws SQLException {

		ReviewBean reviewBean = new ReviewBean();

		reviewBean.setMovie_id(rs.getInt("movie_id"));
		reviewBean.setDate_added(rs.getDate("date_added"));
		reviewBean.setNickname (rs.getString("nickname"));
		reviewBean.setRating(rs.getInt("rating"));
		reviewBean.setReview(rs.getString("review"));

		return reviewBean;
	}
	
}
